package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.bootdo.wx.domain.Config;
import com.bootdo.wx.service.ConfigService;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2018-11-21 10:06:13
 */
 
@Controller
@RequestMapping("/wx/config")
public class ConfigController extends BaseController{
	@Autowired
	private ConfigService configService;
	@ModelAttribute
	public Config get(@RequestParam(required=false) Integer id) {
		if (id != null){
			return configService.get(id);
		}else{
			return new Config();
		}
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(Config config) {
		return R.ok().put("i",config);
	}

	//insert into sys_menu(parent_id,name,url,perms,type)  values (?,'?','/wx/config','wx:config:config',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (?,'添加','/wx/config/add','wx:config:add',2)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (?,'修改','','wx:config:edit',2)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (?,'删除','/wx/config/remove','wx:config:remove',2)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (?,'批量删除','/wx/config/batchRemove','wx:config:batchRemove',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("wx:config:config")	
	String Config(){
	    return "wx/config/config";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("wx:config:config")
	public PageUtils list(@RequestParam Map<String, Object> params){
		
		params.put("userId", getUserId());
		
		//查询列表数据
        Query query = new Query(params);
		List<Config> configList = configService.list(query);
		int total = configService.count(query);
		PageUtils pageUtils = new PageUtils(configList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:config:edit")
	String add(){
	    return "wx/config/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:config:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		Config config = configService.get(id);
		model.addAttribute("config", config);
	    return "wx/config/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:config:edit")
	public R save( Config config){
		config.setUserId(getUserId());
		if(configService.save(config)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:config:edit")
	public R update( Config config){
		configService.update(config);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:config:edit")
	public R remove( Integer id){
		if(configService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:config:edit")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		configService.batchRemove(ids);
		return R.ok();
	}
	
}